﻿using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using ViewModel.Response;

namespace Management.Filters.Filters
{
    [Obsolete("暂时不用")]
    public class ExceptionFilter : IExceptionFilter
    {
        private readonly ILogger<ExceptionFilter> logger;
        private readonly IHostEnvironment environment;

        public ExceptionFilter(ILogger<ExceptionFilter> logger, IHostEnvironment environment)
        {
            this.logger = logger;
            this.environment = environment;
        }

        public void OnException(ExceptionContext context)
        {
            var result = new ResponseModel<string>
            {
                Data = context.Exception.Message,
                Status = ResponseStatus.ServerError,
                Message = this.environment.IsDevelopment() ? context.Exception.Message : "系统发生错误,请联系管理员"
            };

            context.ExceptionHandled = true;
            context.Result = new OkObjectResult(result);
        }
    }
}
